package top.jle.wx.proxy.api.material;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import top.jle.jpa.basic.controller.BaseController;
import top.jle.jpa.basic.dao.PagedResult;
import top.jle.wx.proxy.annotation.AccessToken;
import top.jle.wx.proxy.api.ResponseBack;
import top.jle.wx.proxy.domain.material.Article;
import top.jle.wx.proxy.facade.MaterialFacade;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

import static top.jle.wx.proxy.constant.MaterialOption.*;
@RestController
@RequestMapping("/material")
@Tag(name = "素材管理API")
@AccessToken
public class MaterialAPI extends BaseController {

    @Autowired
    private MaterialFacade materialFacade;

    @Operation(summary = "新增临时素材")
    @PostMapping("/upload_temprory/{type}")
    public ResponseBack<TemporaryMaterialResponse> uploadTemporary(@Parameter(description="素材类型") @PathVariable String type,
                                                                   @Parameter(description="file") @RequestPart MultipartFile file){
        TemporaryMaterialResponse response = materialFacade.addTemporary(type,file);
        return ResponseBack.success(response);
    }

    @Operation(summary = "获取临时素材")
    @PostMapping("/get_temporary")
    public void getTemporary(@RequestParam String mediaId) throws IOException {

        ResponseEntity<byte[]> res =  materialFacade.getTemporary(mediaId);
        getResponse().setContentType(Objects.requireNonNull(res.getHeaders().getContentType()).toString());
        getResponse().setContentLength(Objects.requireNonNull(res.getBody()).length);
        getResponse().getOutputStream().write(res.getBody());

    }

    @Operation(summary = "新增永久素材")
    @PostMapping("/upload/{type}")
    public ResponseBack<MaterialResponse> uploadMaterial(@Parameter(description="素材类型") @PathVariable String type,
                                                         @Parameter(description="file") @RequestPart MultipartFile file,
                                                         @ParameterObject @ModelAttribute VideoDescription videoDescription) {
        MaterialResponse response = materialFacade.addMaterial(type, file, videoDescription);
        return ResponseBack.success(response);
    }


    @Operation(summary = "获取永久素材")
    @GetMapping("/get")
    public void getMaterial(@Parameter(description="mediaId") @RequestParam String mediaId) throws IOException {

        ResponseEntity<byte[]> res =  materialFacade.getMaterial(mediaId);
        HttpHeaders headers = res.getHeaders();
        for(String h:headers.keySet()){
            getResponse().addHeader(h,headers.getFirst(h));
        }
        getResponse().setContentType(Objects.requireNonNull(res.getHeaders().getContentType()).toString());
        getResponse().getOutputStream().write(Objects.requireNonNull(res.getBody()));


    }

    @Operation(summary = "新增图文素材")
    @PostMapping("/add_articles")
    public ResponseBack<MaterialResponse> addArticles(@RequestBody List<Article> articles){
        MaterialResponse response = materialFacade.addNews(articles);
        return ResponseBack.success(response);
    }

    @Operation(summary = "上传图文消息内的图片获取URL")
    @PostMapping("/uploadImg_article")
    public ResponseBack<MaterialResponse> uploadArticleImg(@RequestPart("file") MultipartFile file){
        MaterialResponse response = materialFacade.uploadImgForArticle(file);
        return ResponseBack.success(response);
    }

    @Operation(summary = "更新图文素材")
    @PostMapping("/update_article")
    public ResponseBack<Boolean> addArticles(@Parameter(description="mediaId") @RequestParam String mediaId,
                                             @Parameter(description="index") @RequestParam Integer index,
                                                      @RequestBody Article article){
        materialFacade.updateArticle(mediaId,index,article);
        return ResponseBack.success(true);
    }

    @Operation(summary = "删除永久素材")
    @PostMapping("/del_material")
    public ResponseBack<Boolean> deleteMaterial(@Parameter(description="mediaId") @RequestParam String mediaId){
        materialFacade.deleteMaterial(mediaId);
        return ResponseBack.success(true);
    }

    @Operation(summary = "获取永久素材总数")
    @GetMapping("/material_count")
    public ResponseBack<MaterialCountResponse> getMaterialCount(){
        MaterialCountResponse response = materialFacade.getMaterialCount();
        return ResponseBack.success(response);
    }

    @Operation(summary = "批量获取永久素材")
    @GetMapping("/batch_get")
    public ResponseBack<PagedResult<MaterialItemResponse>> deleteMaterial(@Parameter(description="素材类型") @RequestParam String type,
                                                                          @Parameter(description="页码") @RequestParam Integer page,
                                                                          @Parameter(description="每页条数 最大20") @RequestParam Integer limit){
        if(page==null){
            page=1;
        }
        if(limit==null || limit > 20){
            limit = 20;
        }
        PagedResult<MaterialItemResponse> pagedResult = materialFacade.batchGetMaterial(type,page,limit);
        return ResponseBack.success(pagedResult);
    }

    @Operation(summary = "批量获取图文永久素材")
    @GetMapping("/batch_getArticle")
    public ResponseBack<PagedResult<MaterialArticleItemResponse>> deleteMaterial(@Parameter(description="页码") @RequestParam Integer page,
                                                                                 @Parameter(description="每页条数 最大20") @RequestParam Integer limit){
        if(page==null){
            page=1;
        }
        if(limit==null || limit > 20){
            limit = 20;
        }
        PagedResult<MaterialArticleItemResponse> pagedResult = materialFacade.batchGetArticleMaterial(page,limit);
        return ResponseBack.success(pagedResult);
    }

}
